
 <!DOCTYPE HTML>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  
    <title>React使用笔记7--关于Refs和React表单 | Here. There.</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="被删">
    
    <meta name="description" content="最近在学习使用React作为前端的框架，《React使用笔记》系列用于记录过程中的一些使用和解决方法。本文简单介绍Refs相关，以及记录React表单相关的绑定和focus的过程。">
    
    
    
    
    <link rel="alternate" href="/atom.xml" title="Here. There." type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/img/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/img/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
    
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3d902de4a19cf2bf179534ffd2dd7b7f";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/sun.png" alt="Here. There." title="Here. There."/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="Here. There.">Here. There.</a></h1>
				<h2 class="blog-motto">Love ice cream. Love sunshine. Love life. Love the world. Love myself. Love you.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="菜单">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">首页</a></li>
					
						<li><a href="https://github.com/godbasin/godbasin.github.io">所有文章</a></li>
					
						<li><a href="/archives">归档</a></li>
					
						<li><a href="/categories">分类</a></li>
					
						<li><a href="/about">关于我</a></li>
					
				</ul>
			</nav>			
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2016/08/27/react-notes-7-form-and-ref/" title="React使用笔记7--关于Refs和React表单" itemprop="url">React使用笔记7--关于Refs和React表单</a>
  </h1>
  <p class="article-author">By
    
      <a href="https://godbasin.github.io" title="被删">被删</a>
    </p>
  <p class="article-time">
    <time datetime="2016-08-27T01:10:49.000Z" itemprop="datePublished">2016-08-27</time>
    更新日期:<time datetime="2018-12-18T15:37:47.677Z" itemprop="dateModified">2018-12-18</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">文章目录</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#React-Refs"><span class="toc-number">1.</span> <span class="toc-text">React Refs</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Refs"><span class="toc-number">1.1.</span> <span class="toc-text">Refs</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#findDOMNode"><span class="toc-number">1.2.</span> <span class="toc-text">findDOMNode()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Ref优点"><span class="toc-number">1.3.</span> <span class="toc-text">Ref优点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.4.</span> <span class="toc-text">参考</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Flux数据流"><span class="toc-number">1.5.</span> <span class="toc-text">Flux数据流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考-1"><span class="toc-number">1.6.</span> <span class="toc-text">参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用Ref属性对表单进行focus"><span class="toc-number">2.</span> <span class="toc-text">使用Ref属性对表单进行focus</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Index组件中"><span class="toc-number">2.1.</span> <span class="toc-text">Index组件中</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IndexController组件中"><span class="toc-number">2.2.</span> <span class="toc-text">IndexController组件中</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React表单的onChange"><span class="toc-number">3.</span> <span class="toc-text">React表单的onChange</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#表单事件"><span class="toc-number">3.1.</span> <span class="toc-text">表单事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#添加onChange事件并输出"><span class="toc-number">3.2.</span> <span class="toc-text">添加onChange事件并输出</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">4.</span> <span class="toc-text">结束语</span></a></li></ol>
		</div>
		
		<p>最近在学习使用React作为前端的框架，《React使用笔记》系列用于记录过程中的一些使用和解决方法。<br>本文简单介绍Refs相关，以及记录React表单相关的绑定和focus的过程。<br><a id="more"></a></p>
<h2 id="React-Refs"><a href="#React-Refs" class="headerlink" title="React Refs"></a>React Refs</h2><hr>
<h3 id="Refs"><a href="#Refs" class="headerlink" title="Refs"></a>Refs</h3><p>为了获取一个到React组件的引用，你可以使用this来得到当前的React组件，或者你可以使用refs来指向一个你拥有的组件。</p>
<ul>
<li>React支持一种非常特殊的属性，你可以用来绑定到render()输出的任何组件上去。</li>
<li>Ref属性允许你引用render()返回的相应的支撑实例（ backing instance ）。<br>这样就可以确保在任何时间总是拿到正确的实例。</li>
<li>做法很简单：<ul>
<li>1.绑定一个ref属性到render返回的东西上面去，例如：</li>
</ul>
</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;input ref=<span class="string">"myInput"</span> /&gt;</div></pre></td></tr></table></figure>
<ul>
<li>2、在其它代码中（典型地事件处理代码），通过this.refs获取支撑实例（ backing instance ），就像这样：</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">this</span>.refs.myInput</div></pre></td></tr></table></figure>
<h3 id="findDOMNode"><a href="#findDOMNode" class="headerlink" title="findDOMNode()"></a>findDOMNode()</h3><p>为了和浏览器交互，你将需要对DOM节点的引用。React提供了React.findDOMNode(component)函数，你可以调用这个函数来获取该组件的DOM结点。</p>
<ul>
<li>注意：React v0.14以上版本中，需使用ReactDOM.findDOMNode(component)，且需引入react-don。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> ReactDOM <span class="keyword">from</span> <span class="string">'react-dom'</span>; <span class="comment">//react-dom</span></div></pre></td></tr></table></figure>
<ul>
<li>findDOMNode()仅在挂载的组件上有效（也就是说，组件已经被放进了DOM中）。如果你尝试在一个未被挂载的组件上调用这个函数（例如在创建组件的render()函数中调用findDOMNode()），将会抛出异常。</li>
<li>从 render() 中返回的内容并不是实际渲染出来的子组件实例。从 render() 返回的仅仅是子组件层级树实例在特定时间的一个描述。可参考<a href="/2016/08/13/react-notes-3-props-state-lifecycle/">组件的生命周期</a>。</li>
</ul>
<h3 id="Ref优点"><a href="#Ref优点" class="headerlink" title="Ref优点"></a>Ref优点</h3><p>Refs是一种给指定的子组件实例发送消息的很好的方式，从某种程度上来看，通过props和state来做这件事倒显得不太方便。</p>
<ul>
<li>可以在组件类里面定义任何公共的方法（比如在输入之前的重置方法），然后通过refs来调用这些公共的方法（比如this.refs.myTypeahead.reset() ）。</li>
<li>管理DOM几乎总是需要冲出“本地”组件的限制，比如通过this.refs.myInput.getDOMNode()获取<code>&lt;input /&gt;</code>元素的底层DOM节点。Refs是做这件事唯一可靠的方式。</li>
<li>Refs是被自动管理的！如果某个子级实例被销毁了，它的ref也会自动销毁。不用考虑内存问题（除非你自己做一些疯狂的操作，保存了什么引用）。</li>
</ul>
<h3 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h3><p><a href="http://reactjs.cn/react/docs/more-about-refs.html" target="_blank" rel="external">《关于Refs的更多内容》</a></p>
<h3 id="Flux数据流"><a href="#Flux数据流" class="headerlink" title="Flux数据流"></a>Flux数据流</h3><p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/bg2016011503.png" alt="image"></p>
<ul>
<li>store在dispatcher中注册，并提供相应的回调。回调会接收action并把它当成自己的一个参数。</li>
<li>通过调用action来响应用户交互。</li>
<li>当action被触发，回调函数会使用switch语句来解析action中的type参数，并在合适的type下提供钩子来执行内部方法。</li>
<li>action通过dispatcher来响应store中的state更新。</li>
<li>store更新完成之后，会向应用中广播一个change事件，views可以选择响应事件来重新获取新的数据并更新。</li>
</ul>
<h3 id="参考-1"><a href="#参考-1" class="headerlink" title="参考"></a>参考</h3><p><a href="http://www.ruanyifeng.com/blog/2016/01/flux.html" target="_blank" rel="external">《Flux 架构入门教程》</a><br><a href="http://www.cocoachina.com/webapp/20150928/13600.html" target="_blank" rel="external">《谈一谈我对 React Flux 架构的理解》</a></p>
<h2 id="使用Ref属性对表单进行focus"><a href="#使用Ref属性对表单进行focus" class="headerlink" title="使用Ref属性对表单进行focus"></a>使用Ref属性对表单进行focus</h2><hr>
<p>这里简述使用Ref属性对表单进行focus的过程。</p>
<h3 id="Index组件中"><a href="#Index组件中" class="headerlink" title="Index组件中"></a>Index组件中</h3><p>在index的修改资料部分，添加表单，这里使用了react-bootstrap。</p>
<ul>
<li>给两个input分别加上ref属性</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;FormControl type=<span class="string">"email"</span> placeholder=<span class="string">"Email"</span> ref=<span class="string">"email"</span> required /&gt;</div><div class="line"><span class="xml"><span class="tag">&lt;<span class="name">FormControl</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">placeholder</span>=<span class="string">"Name"</span> <span class="attr">ref</span>=<span class="string">"name"</span> <span class="attr">required</span> /&gt;</span></span></div></pre></td></tr></table></figure>
<ul>
<li>添加两个按钮，绑定点击对input进行focus</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;Button onClick=&#123;this.props.focusInput.bind(null, 'email')&#125;&gt;focus邮件&lt;/Button&gt;</div><div class="line">&lt;Button onClick=&#123;this.props.focusName&#125;&gt;focus名字&lt;/Button&gt;</div></pre></td></tr></table></figure>
<h3 id="IndexController组件中"><a href="#IndexController组件中" class="headerlink" title="IndexController组件中"></a>IndexController组件中</h3><ul>
<li>添加子组件ref属性<ul>
<li>在父组件中获取子组件的refs，可通过给子组件添加ref属性来获取子组件，再获取其refs</li>
<li>该方法可以用在父组件和子组件之间的通信，父组件获取子组件的数据</li>
</ul>
</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Index ref=<span class="string">"index"</span> /&gt;</div></pre></td></tr></table></figure>
<ul>
<li>添加事件focusInpu和focusName</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">focusInput: <span class="function"><span class="keyword">function</span>(<span class="params">name</span>)</span>&#123;</div><div class="line">  ReactDOM.findDOMNode(<span class="keyword">this</span>.refs.index.refs[name]).focus(); </div><div class="line">&#125;,</div><div class="line"><span class="attr">focusName</span>: <span class="function"><span class="keyword">function</span>(<span class="params">name</span>)</span>&#123;</div><div class="line">  ReactDOM.findDOMNode(<span class="keyword">this</span>.refs.index.refs.name).focus(); 		</div><div class="line">&#125;,</div></pre></td></tr></table></figure>
<p>这里我们可以看到，refs的获取有两种方法，可以通过refs[name]和refs.name来获取。第一种方法可以帮助我们创建通用的获取方法。</p>
<ul>
<li>通过props传递方法给子组件<ul>
<li>该方法可用于父组件和子组件之间的通信，子组件获取父组件的数据</li>
</ul>
</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Index ref=<span class="string">"index"</span> focusInput=&#123;<span class="keyword">this</span>.focusInput&#125; focusName=&#123;<span class="keyword">this</span>.focusName&#125; /&gt;</div></pre></td></tr></table></figure>
<p>此时我们完成了使用refs来控制组件元素的过程。下面将简单讲述一下React中表单组件的onChange事件。</p>
<h2 id="React表单的onChange"><a href="#React表单的onChange" class="headerlink" title="React表单的onChange"></a>React表单的onChange</h2><hr>
<p>诸如<code>&lt;input&gt;</code>、<code>&lt;textarea&gt;</code>、<code>&lt;option&gt;</code>这样的表单组件不同于其他组件，因为他们可以通过用户交互发生变化。这些组件提供的界面使响应用户交互的表单数据处理更加容易。</p>
<h3 id="表单事件"><a href="#表单事件" class="headerlink" title="表单事件"></a>表单事件</h3><p>React中表单事件分别有：</p>
<ul>
<li>onChange: 执行并通过浏览器做出以下响应<ul>
<li><code>&lt;input&gt;</code>或<code>&lt;textarea&gt;</code>的value发生变化时</li>
<li><code>&lt;input&gt;</code>的checked状态改变时</li>
<li><code>&lt;option&gt;</code>的selected状态改变时</li>
</ul>
</li>
<li>onInput: 只作用于<code>&lt;input&gt;</code>或<code>&lt;textarea&gt;</code>的value发生变化时</li>
<li>onSubmit: 表单提交时执行</li>
</ul>
<h3 id="添加onChange事件并输出"><a href="#添加onChange事件并输出" class="headerlink" title="添加onChange事件并输出"></a>添加onChange事件并输出</h3><ul>
<li>Index组件中，添加value值，以及绑定onChange事件。（由父组件通过props传递）</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">&lt;FormControl type=<span class="string">"email"</span> ref=<span class="string">"email"</span> value=&#123;<span class="keyword">this</span>.props.email&#125; onChange=&#123;<span class="keyword">this</span>.props.changeInput.bind(<span class="literal">null</span>, <span class="string">'email'</span>)&#125; required /&gt;</div><div class="line"><span class="xml"><span class="tag">&lt;<span class="name">FormControl</span> <span class="attr">type</span>=<span class="string">"text"</span> <span class="attr">ref</span>=<span class="string">"name"</span> <span class="attr">value</span>=<span class="string">&#123;this.props.name&#125;</span> <span class="attr">onChange</span>=<span class="string">&#123;this.props.changeInput.bind(null,</span> '<span class="attr">name</span>')&#125; <span class="attr">required</span> /&gt;</span></span></div></pre></td></tr></table></figure>
<ul>
<li>IndexController组件中，添加email和name作为状态state，以及事件changeInput</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">getInitialState: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</div><div class="line">  <span class="keyword">return</span> &#123;</div><div class="line">    <span class="attr">name</span>: <span class="string">''</span>,</div><div class="line">    <span class="attr">email</span>: <span class="string">''</span>,</div><div class="line">  &#125;;</div><div class="line">&#125;,</div><div class="line"><span class="attr">changeInput</span>: <span class="function"><span class="keyword">function</span>(<span class="params">name, event</span>) </span>&#123; <span class="comment">//通过event获取当前事件，总是最后一个参数传入</span></div><div class="line">  <span class="keyword">switch</span>(name)&#123;</div><div class="line">    <span class="keyword">case</span> <span class="string">'name'</span>:</div><div class="line">      <span class="comment">//通过event.target获取当前元素，再获取value</span></div><div class="line">      <span class="keyword">this</span>.setState(&#123;<span class="attr">name</span>: event.target.value&#125;); </div><div class="line">      <span class="keyword">break</span>;</div><div class="line">    <span class="keyword">case</span> <span class="string">'email'</span>:</div><div class="line">      <span class="keyword">this</span>.setState(&#123;<span class="attr">email</span>: event.target.value&#125;);</div><div class="line">      <span class="keyword">break</span>;</div><div class="line">  &#125;		</div><div class="line">&#125;,</div></pre></td></tr></table></figure>
<ul>
<li>IndexController组件中，通过props传递事件以及值给Index组件</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">&lt;Index ref=<span class="string">"index"</span> asidemenus=&#123;<span class="keyword">this</span>.props.asidemenus&#125; loading=&#123;<span class="keyword">this</span>.state.loading&#125; changeInput=&#123;<span class="keyword">this</span>.changeInput&#125; focusInput=&#123;<span class="keyword">this</span>.focusInput&#125; focusName=&#123;<span class="keyword">this</span>.focusName&#125; email=&#123;<span class="keyword">this</span>.state.email&#125; name=&#123;<span class="keyword">this</span>.state.name&#125; /&gt;</div></pre></td></tr></table></figure>
<ul>
<li>在flux中通过单向数据流处理<br>上一节我们<a href="/2016/08/21/react-notes-6-use-flux/">将react通过flux进行了改造</a>，现在我们需要将以上的方法调整来符合flux的单向数据流。具体方法步骤就不介绍了，大家可以到代码中查看。</li>
</ul>
<h2 id="结束语"><a href="#结束语" class="headerlink" title="结束语"></a>结束语</h2><hr>
<p>不像Angular的双向绑定，React是单向的数据绑定，故在获取表单值的时候需要另外进行处理呢。<br>React中组件的props和refs属性在父子组件间的通信上有很大的作用哦。<br><a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/react-notes/7-form-and-ref" target="_blank" rel="external">此处查看项目代码（仅包含app部分）</a><br><a href="http://react-notes.godbasin.com/7-form-and-ref/index.html" target="_blank" rel="external">此处查看页面效果</a></p>
  
		
		<p style="margin-top:50px;">查看Github有更多内容噢：<a href="https://github.com/godbasin">https://github.com/godbasin</a></p>
	</div>
	<img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" width="200" height="200" style="display:block;margin: 0 auto;" />
	<p style="text-align: center;margin-top: 10px;margin-bottom: 20px;">码生艰难，写文不易，给我家猪囤点猫粮了喵~</p>
	<div class="author-right">
  		<p>作者：被删</p>
  		<p>出处：<a href="https://godbasin.github.io">https://godbasin.github.io</a></p>
  		<p>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</p>
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/笔记/">笔记</a>
  </div>


<div class="article-categories">
  <span></span>
  <a class="article-category-link" href="/categories/react沙拉/">react沙拉</a>
</div>



<div class="article-share" id="share">

  
<!-- JiaThis Button BEGIN -->
<div class="jiathis_style_24x24">
	<a class="jiathis_button_qzone"></a>
	<a class="jiathis_button_tsina"></a>
	<a class="jiathis_button_tqq"></a>
	<a class="jiathis_button_weixin"></a>
	<a class="jiathis_button_renren"></a>
	<a href="http://www.jiathis.com/share?uid=2134021" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
</div>
<script type="text/javascript">
var jiathis_config = {data_track_clickback:'true'};
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code_mini/jia.js?uid=2134021" charset="utf-8"></script>
<!-- JiaThis Button END -->   


</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2016/08/28/react-notes-8-conmunicate-in-react/" title="React使用笔记8--组件间的通信">
  <strong>PREVIOUS:</strong><br/>
  <span>
  React使用笔记8--组件间的通信</span>
</a>
</div>


<div class="next">
<a href="/2016/08/21/react-notes-6-use-flux/"  title="React使用笔记6--使用flux&quot;单向流动&quot;你的应用">
 <strong>NEXT:</strong><br/> 
 <span>React使用笔记6--使用flux&quot;单向流动&quot;你的应用
</span>
</a>
</div>

</nav>

	<!-- 如果不是首页且没使用 `comments: false` 关闭评论，则尝试加载评论 -->

    <!-- 配置中启用多说时，导入相应代码 -->
    


</div>  
      <div class="openaside"><a class="navbutton" href="#" title="显示侧边栏"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">文章目录</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#React-Refs"><span class="toc-number">1.</span> <span class="toc-text">React Refs</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Refs"><span class="toc-number">1.1.</span> <span class="toc-text">Refs</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#findDOMNode"><span class="toc-number">1.2.</span> <span class="toc-text">findDOMNode()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Ref优点"><span class="toc-number">1.3.</span> <span class="toc-text">Ref优点</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考"><span class="toc-number">1.4.</span> <span class="toc-text">参考</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Flux数据流"><span class="toc-number">1.5.</span> <span class="toc-text">Flux数据流</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#参考-1"><span class="toc-number">1.6.</span> <span class="toc-text">参考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#使用Ref属性对表单进行focus"><span class="toc-number">2.</span> <span class="toc-text">使用Ref属性对表单进行focus</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Index组件中"><span class="toc-number">2.1.</span> <span class="toc-text">Index组件中</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IndexController组件中"><span class="toc-number">2.2.</span> <span class="toc-text">IndexController组件中</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#React表单的onChange"><span class="toc-number">3.</span> <span class="toc-text">React表单的onChange</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#表单事件"><span class="toc-number">3.1.</span> <span class="toc-text">表单事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#添加onChange事件并输出"><span class="toc-number">3.2.</span> <span class="toc-text">添加onChange事件并输出</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#结束语"><span class="toc-number">4.</span> <span class="toc-text">结束语</span></a></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="隐藏侧边栏"></a></div>
<aside class="clearfix">

  	
	<div class="archiveslist">
		<p class="asidetitle">最近文章</p>
			<ul class="archive-list">
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/07/11/vue-for-everyone-2/" title="9102全员学Vue--2.怎么三两下拼出一个页面">9102全员学Vue--2.怎么三两...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/30/about-front-end-2-principle/" title="前端这几年--2.工作原则和选择">前端这几年--2.工作原则和选择...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/27/vue-for-everyone-1/" title="9102全员学Vue--1.如何理解前端和Vue">9102全员学Vue--1.如何理解...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/17/wxapp-latest-20190617/" title="小程序开发月刊第六期（20190617）">小程序开发月刊第六期（2019061...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/06/17/wxapp-generate/" title="小程序的诞生">小程序的诞生</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/05/15/wxapp-latest-20190515/" title="小程序开发月刊第五期（20190515）">小程序开发月刊第五期（2019051...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/04/15/wxapp-latest-20190415/" title="小程序开发月刊第四期（20190415）">小程序开发月刊第四期（2019041...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/04/13/about-front-end-1-begin-in/" title="前端这几年--1.转岗之路">前端这几年--1.转岗之路...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/03/15/wxapp-latest-20190315/" title="小程序开发月刊第三期（20190315）">小程序开发月刊第三期（2019031...</a>
				</li>
				
			
					
				<li class="archive-list-item">
					<a class="archive-list-link" href="/2019/03/10/work-2-article/" title="写文章这件事">写文章这件事</a>
				</li>
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
				
			
			</ul>
	</div>


  
  <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">归档</a></p>
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/07/">七月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/06/">六月 2019</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/05/">五月 2019</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/04/">四月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/03/">三月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/02/">二月 2019</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2019/01/">一月 2019</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/12/">十二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/11/">十一月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/10/">十月 2018</a><span class="archive-list-count">1</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/09/">九月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/08/">八月 2018</a><span class="archive-list-count">2</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/07/">七月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/06/">六月 2018</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/05/">五月 2018</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/04/">四月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/03/">三月 2018</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/02/">二月 2018</a><span class="archive-list-count">4</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2018/01/">一月 2018</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/12/">十二月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/11/">十一月 2017</a><span class="archive-list-count">3</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/10/">十月 2017</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/09/">九月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/08/">八月 2017</a><span class="archive-list-count">11</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/07/">七月 2017</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/06/">六月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/05/">五月 2017</a><span class="archive-list-count">15</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/04/">四月 2017</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/03/">三月 2017</a><span class="archive-list-count">10</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/02/">二月 2017</a><span class="archive-list-count">41</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2017/01/">一月 2017</a><span class="archive-list-count">6</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/12/">十二月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/11/">十一月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/10/">十月 2016</a><span class="archive-list-count">5</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/09/">九月 2016</a><span class="archive-list-count">7</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/08/">八月 2016</a><span class="archive-list-count">9</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/07/">七月 2016</a><span class="archive-list-count">14</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/06/">六月 2016</a><span class="archive-list-count">9</span></li></ul>
  </div>


  
<div class="archiveslist">
	<p class="asidetitle"><a href="/categories">分类</a></p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/CSS炒饭/" title="CSS炒饭">CSS炒饭<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/D3小馒头/" title="D3小馒头">D3小馒头<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular2火锅/" title="angular2火锅">angular2火锅<sup>25</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/angular混搭/" title="angular混搭">angular混搭<sup>33</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/box2djs方糖/" title="box2djs方糖">box2djs方糖<sup>34</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/cyclejs哈根达斯/" title="cyclejs哈根达斯">cyclejs哈根达斯<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/jQuery杂烩/" title="jQuery杂烩">jQuery杂烩<sup>3</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/js什锦/" title="js什锦">js什锦<sup>26</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/react沙拉/" title="react沙拉">react沙拉<sup>16</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/three-js奶茶/" title="three.js奶茶">three.js奶茶<sup>5</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/vue八宝粥/" title="vue八宝粥">vue八宝粥<sup>28</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/webpack宾治/" title="webpack宾治">webpack宾治<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/web乱炖/" title="web乱炖">web乱炖<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/前端满汉全席/" title="前端满汉全席">前端满汉全席<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/喵/" title="喵">喵<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/小程序双皮奶/" title="小程序双皮奶">小程序双皮奶<sup>21</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/工作这杯茶/" title="工作这杯茶">工作这杯茶<sup>4</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/应用巧克力盒/" title="应用巧克力盒">应用巧克力盒<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/思想棉花糖/" title="思想棉花糖">思想棉花糖<sup>8</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/柴米油盐工具集/" title="柴米油盐工具集">柴米油盐工具集<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/categories/非前端钙片/" title="非前端钙片">非前端钙片<sup>7</sup></a>
			</li>
		
		</ul>
</div>


  
<div class="archiveslist">
	<p class="asidetitle">标签</p>
		<ul class="archive-list">
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/分享/" title="分享">分享<sup>44</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/原创部件/" title="原创部件">原创部件<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/心态/" title="心态">心态<sup>4</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/教程/" title="教程">教程<sup>65</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/晒猫/" title="晒猫">晒猫<sup>1</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/笔记/" title="笔记">笔记<sup>121</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/算法/" title="算法">算法<sup>9</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/自制插件/" title="自制插件">自制插件<sup>2</sup></a>
			</li>
		
			<li class="archive-list-item">
				<a class="archive-list-link" href="/tags/逻辑实现/" title="逻辑实现">逻辑实现<sup>5</sup></a>
			</li>
		
		</ul>
</div>


    <div class="archiveslist">
    <p class="asidetitle"><a href="/archives">about</a></p>
      <ul class="archive-list">
      	<li class="archive-list-item">
      		<a>wangbeishan@163.com</a>
      		<a href="https://github.com/godbasin">github.com/godbasin</a>
      	</li>
      </ul>
  </div>

  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS 订阅</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<section class="info">
		<p> 即使梦想再小，却很伟大 ^_^ </p>
	</section>
	 
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2019 
		
		<a href="https://godbasin.github.io" target="_blank" title="被删">被删</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>







  </body>
</html>
